package com.bwie.user.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.TypeDao;
import com.bwie.user.entity.TypeEntity;
import com.bwie.user.service.TypeService;


@Service("typeService")
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        BoundHashOperations map = redisTemplate.boundHashOps("map2");
        PageUtils typeList = (PageUtils) map.get("typeList");
        if (typeList==null){
            System.out.println("从数据库查商品类型");
            IPage<TypeEntity> page = this.page(
                    new Query<TypeEntity>().getPage(params),
                    new QueryWrapper<TypeEntity>()
            );


            typeList=new PageUtils(page);
            map.put("typeList",typeList);
        }

//        System.out.println("缓存中查商品类型");
        return typeList;
    }
    //商品搜索系统运行期间，使用定时器每隔30秒，同步一次商品类型、商品来源信息到Redis当中
    @Scheduled(cron = "0/30 * * * * *")
    public void ds2(){
        Object o = redisTemplate.boundHashOps("map2").get("typeList");
        String s = JSON.toJSONString(o);
        System.out.println(new Date()+"定时任务+缓存中查找"+s);

    }

}